package org.jeecg.modules.xl.product.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.modules.xl.exceltemplate.vo.ProductTypeVo;
import org.jeecg.modules.xl.order.vo.ProductSheet;
import org.jeecg.modules.xl.product.entity.XlProductCustomerPrice;
import org.jeecg.modules.xl.product.entity.XlProductInfo;
import org.jeecg.modules.xl.product.entity.XlProductPics;
import org.jeecg.modules.xl.product.mapper.XlProductCustomerPriceMapper;
import org.jeecg.modules.xl.product.mapper.XlProductInfoMapper;
import org.jeecg.modules.xl.product.mapper.XlProductPicsMapper;
import org.jeecg.modules.xl.product.service.IXlProductInfoService;
import org.jeecg.modules.xl.producttype.entity.XlProductType;
import org.jeecg.modules.xl.producttype.mapper.XlProductTypeMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;
import java.util.Map;

/**
 * @Description: xl_product_info
 * @Author: jeecg-boot
 * @Date:   2024-10-18
 * @Version: V1.0
 */
@Service
public class XlProductInfoServiceImpl extends ServiceImpl<XlProductInfoMapper, XlProductInfo> implements IXlProductInfoService {

	@Autowired
	private XlProductInfoMapper xlProductInfoMapper;
	@Autowired
	private XlProductPicsMapper xlProductPicsMapper;
	@Autowired
	private XlProductTypeMapper xlProductTypeMapper;
	@Autowired
	private XlProductCustomerPriceMapper xlProductCustomerPriceMapper;
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(XlProductInfo xlProductInfo, List<XlProductPics> xlProductPicsList) {
		ProductTypeVo productTypeVo = new ProductTypeVo();//xlProductTypeMapper.queryTypeTreeById(xlProductInfo.getProductType());
		xlProductInfo.setL1Type(productTypeVo.getL1type());
		xlProductInfo.setL2Type(productTypeVo.getL2type());
		xlProductInfoMapper.insert(xlProductInfo);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(XlProductInfo xlProductInfo,List<XlProductPics> xlProductPicsList) {
		xlProductInfoMapper.updateById(xlProductInfo);
//		//1.先删除子表数据
//		xlProductPicsMapper.deleteByMainId(xlProductInfo.getId());
//
//		//2.子表数据重新插入
//		if(xlProductPicsList!=null && xlProductPicsList.size()>0) {
//			for(XlProductPics entity:xlProductPicsList) {
//				//外键设置
//				entity.setId(xlProductInfo.getId());
//				xlProductPicsMapper.insert(entity);
//			}
//		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		xlProductPicsMapper.deleteByMainId(id);
		xlProductInfoMapper.deleteById(id);
		//也需要删除customer里的数据xl_product_customer_price
		QueryWrapper<XlProductCustomerPrice> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("product_id",id);
		xlProductCustomerPriceMapper.delete(queryWrapper);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			xlProductPicsMapper.deleteByMainId(id.toString());
			xlProductInfoMapper.deleteById(id);
			QueryWrapper<XlProductCustomerPrice> queryWrapper = new QueryWrapper<>();
			queryWrapper.eq("product_id",id);
			xlProductCustomerPriceMapper.delete(queryWrapper);
		}
	}


	public List<ProductSheet> getProductListForExport(){
		return baseMapper.getProductListForExport();
	}

	@Override
	public List<ProductSheet> getProductWithPriceListByStoreLevel(String priceLevel) {
		return baseMapper.getProductWithPriceListByStoreLevel(priceLevel);
	}

	@Override
	public Map<String,Map<String, String>> getCoverCustomer(String upccodes) {
		return baseMapper.getCoverCustomer(upccodes);
	}
	@Value("${pichost}")
	private String baseUrl;

	@Override
	@Cacheable(cacheNames = "productInfo#3600",unless="#result == null" ,key = "#upc_code")
	public XlProductInfo getProductInfoCacheable(String upc_code){
		QueryWrapper<XlProductInfo> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("upc_code",upc_code);
		XlProductInfo xlProductInfo = baseMapper.selectOne(queryWrapper);
		if(ObjectUtil.isEmpty(xlProductInfo)){
			return null;
		}
		if(ObjectUtil.isAllEmpty(xlProductInfo,xlProductInfo.getMainPic())){
			xlProductInfo.setMainPic(baseUrl+ "/temp/nophoto.png");
		}else{
			xlProductInfo.setMainPic(baseUrl + xlProductInfo.getMainPic());
		}
		return  xlProductInfo;
	}
}
